package com.xiaobai.netease_news.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.activities.PlayerActivity;
import com.xiaobai.netease_news.activities.PlayerItemActivity;
import com.xiaobai.netease_news.model.Clist;
import com.xiaobai.netease_news.model.Radio;
import com.xiaobai.netease_news.model.RadioResponseData;
import com.xiaobai.netease_news.model.Tlist;
import com.xiaobai.netease_news.model.Top;
import com.xiaobai.netease_news.utils.RequestManager;
import com.xiaobai.netease_news.utils.ViewHolder;

import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;


public class RadioFragment extends Fragment
        implements PullToRefreshListView.OnRefreshListener2, Runnable,
        AdapterView.OnItemClickListener {

    private PullToRefreshListView ptrListView;
    private List<Clist> clistList;
    private List<Top> topList;
    private MyAdapter adapter;
    View topView;
    private ImageView imageView;

    public RadioFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        clistList = new LinkedList<>();
        topList = new LinkedList<>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("onCreate", "onCreate");
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_radio, container, false);
        ptrListView = (PullToRefreshListView) view.findViewById(R.id.radio_listView);
        ListView listView = ptrListView.getRefreshableView();
        adapter = new MyAdapter(clistList);
        topView = getHeadView();
        listView.addHeaderView(topView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        ptrListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        ptrListView.setOnRefreshListener(this);
        return view;
    }

    private View getHeadView() {
        topView = LayoutInflater.from(getActivity()).inflate(R.layout.radio_headview, null);
        imageView = (ImageView) topView.findViewById(R.id.radio_headView_image);
        return topView;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("onResume", "onResume");
        RequestQueue requestQueue = RequestManager.getInstance(getActivity()).getRequestQueue();
        String url = "http://c.m.163.com/nc/topicset/android/radio/index.html";
        JsonObjectRequest request = new JsonObjectRequest(
                url,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        RadioResponseData radioResponseData = new RadioResponseData();
                        radioResponseData.parseJSON(jsonObject);
                        List<Clist> clistLists = radioResponseData.getClistList();
                        clistList.clear();
                        clistList.addAll(0, clistLists);
                        List<Top> topLists = radioResponseData.getTopList();
                        ImageLoader imageLoader = RequestManager.getInstance(getActivity()).getImageLoader();
                        if (!topLists.isEmpty()) {
                            String imgsrc = topLists.get(0).getRadio().getImgsrc();
                            imageLoader.get(
                                    imgsrc,
                                    imageLoader.getImageListener(imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
                            );
                        }
                        Log.d("clistList", "clistList:" + clistList);
                        adapter.notifyDataSetChanged();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        volleyError.printStackTrace();
                    }
                }
        );
        requestQueue.add(request);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void run() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String cid = clistList.get(position - 2).getCid();
        String cname = clistList.get(position - 2).getCname();
        Intent intent = new Intent();
        intent.putExtra("cid", cid);
        intent.putExtra("cname", cname);
        Log.d("cid", "cid:" + cid);
        intent.setClass(getActivity(), PlayerItemActivity.class);
        startActivity(intent);
    }

    public class MyAdapter extends BaseAdapter implements View.OnClickListener {

        private List<Clist> clistList;
        private List<Tlist> tlistList;

        public MyAdapter(List<Clist> clistList) {
            this.clistList = clistList;
        }

        @Override
        public int getCount() {
            int ret = 0;
            if (clistList != null) {
                ret = clistList.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            Object ret = null;
            if (clistList != null) {
                ret = clistList.get(position);
            }
            return ret;
        }

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public long getItemId(int position) {
            long ret = 0;
            if (clistList != null) {
                ret = position;
            }
            return ret;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View ret;
            if (convertView != null) {
                ret = convertView;
            } else {
                ret = LayoutInflater.from(getActivity()).inflate(R.layout.radio_adatper_item, parent, false);
            }
//            总标题
            TextView txtTitle = ViewHolder.getView(ret, R.id.radio_item_txtTitle);
            ImageView imageHead = ViewHolder.getView(ret, R.id.radio_item_headImage);
//            标题点击事件
            View btnContent1 = ret.findViewById(R.id.radio_item_btnContent1);
            btnContent1.setTag(position);
            btnContent1.setOnClickListener(this);

            View btnContent2 = ret.findViewById(R.id.radio_item_btnContent2);
            btnContent2.setTag(position);
            btnContent2.setOnClickListener(this);

            View btnContent3 = ret.findViewById(R.id.radio_item_btnContent3);
            btnContent3.setTag(position);
            btnContent3.setOnClickListener(this);

//            播放数量
            TextView txtCount1 = ViewHolder.getView(ret, R.id.radio_item_txtCount1);
            TextView txtCount2 = ViewHolder.getView(ret, R.id.radio_item_txtCount2);
            TextView txtCount3 = ViewHolder.getView(ret, R.id.radio_item_txtCount3);

//            图片
            ImageView imageContent1 = ViewHolder.getView(ret, R.id.radio_item_imageContent1);
            ImageView imageContent2 = ViewHolder.getView(ret, R.id.radio_item_imageContent2);
            ImageView imageContent3 = ViewHolder.getView(ret, R.id.radio_item_imageContent3);

//            标题
            TextView txtTitle1 = ViewHolder.getView(ret, R.id.radio_item_name1);
            TextView txtTitle2 = ViewHolder.getView(ret, R.id.radio_item_name2);
            TextView txtTitle3 = ViewHolder.getView(ret, R.id.radio_item_name3);

//            内容
            TextView txtContent1 = ViewHolder.getView(ret, R.id.radio_item_content1);
            TextView txtContent2 = ViewHolder.getView(ret, R.id.radio_item_content2);
            TextView txtContent3 = ViewHolder.getView(ret, R.id.radio_item_content3);
            tlistList = clistList.get(position).getTlistList();
            Log.d("tlistList", "tlistList:" + clistList);
//            设置总标题
            txtTitle.setText(clistList.get(position).getCname());

//            设置播放数目
            if (!tlistList.isEmpty()) {
                long playCount1 = tlistList.get(0).getPlayCount();
                txtCount1.setText(String.valueOf(playCount1));
                long playCount2 = tlistList.get(1).getPlayCount();
                txtCount2.setText(String.valueOf(playCount2));
                long playCount3 = tlistList.get(2).getPlayCount();
                txtCount3.setText(String.valueOf(playCount3));

//            设置图片
                Radio radio1 = tlistList.get(0).getRadio();
                String imgsrc1 = radio1.getImgsrc();
                ImageLoader imageLoader = RequestManager.getInstance(getActivity()).getImageLoader();
                imageLoader.get(
                        imgsrc1,
                        imageLoader.getImageListener(imageContent1, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
                );

                Radio radio2 = tlistList.get(1).getRadio();
                String imgsrc2 = radio2.getImgsrc();
                imageLoader.get(
                        imgsrc2,
                        imageLoader.getImageListener(imageContent2, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
                );

                Radio radio3 = tlistList.get(2).getRadio();
                String imgsrc3 = radio3.getImgsrc();
                imageLoader.get(
                        imgsrc3,
                        imageLoader.getImageListener(imageContent3, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
                );

                imageLoader.get(
                        imgsrc3,
                        imageLoader.getImageListener(imageHead, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
                );

//            设置标题
                String tname1 = tlistList.get(0).getTname();
                txtTitle1.setText(tname1);

                String tname2 = tlistList.get(1).getTname();
                txtTitle2.setText(tname2);

                String tname3 = tlistList.get(2).getTname();
                txtTitle3.setText(tname3);

//            设置内容
                String content1 = radio1.getTitle();
                txtContent1.setText(content1);
                String content2 = radio2.getTitle();
                txtContent2.setText(content2);
                String content3 = radio3.getTitle();
                txtContent3.setText(content3);
            }
            return ret;
        }

        @Override
        public void onClick(View v) {
            Object tag = v.getTag();
            int id = v.getId();
            switch (id) {
                case R.id.radio_item_btnContent1:
                    if (tag != null && tag instanceof Integer) {
                        Integer it = (Integer) tag;
                        String tid = clistList.get(it).getTlistList().get(0).getTid();
                        Log.d("it", "it:" + it);
                        Intent intent = new Intent();
                        intent.putExtra("tid", tid);
                        intent.setClass(getActivity(), PlayerActivity.class);
                        startActivity(intent);

                    }
                    break;
                case R.id.radio_item_btnContent2:
                    if (tag != null && tag instanceof Integer) {
                        Integer it = (Integer) tag;
                        String tid = clistList.get(it).getTlistList().get(1).getTid();
                        Intent intent = new Intent();
                        intent.putExtra("tid", tid);
                        intent.setClass(getActivity(), PlayerActivity.class);
                        startActivity(intent);

                    }
                    break;
                case R.id.radio_item_btnContent3:
                    if (tag != null && tag instanceof Integer) {
                        Integer it = (Integer) tag;
                        String tid = clistList.get(it).getTlistList().get(2).getTid();
                        Intent intent = new Intent();
                        intent.putExtra("tid", tid);
                        intent.setClass(getActivity(), PlayerActivity.class);
                        startActivity(intent);
                    }
                    break;
            }
        }
    }
}
